package com.fly.docker.job;

import java.net.URI;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerCertificates;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerClient.ListContainersParam;
import com.spotify.docker.client.exceptions.DockerCertificateException;
import com.spotify.docker.client.exceptions.DockerException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ScheduleJob
{
    DockerClient dockerClient = null;
    
    @PostConstruct
    private void init()
    {
        try
        {
            if (SystemUtils.IS_OS_UNIX)
            {
                dockerClient = DefaultDockerClient.builder().uri(URI.create("https://118.178.86.130:2375")).dockerCertificates(new DockerCertificates(Paths.get("/etc/docker/"))).build();
            }
            else if (SystemUtils.IS_OS_WINDOWS)
            {
                dockerClient = DefaultDockerClient.builder().uri(URI.create("https://118.178.86.130:2375")).dockerCertificates(new DockerCertificates(Paths.get("/docker_ca/"))).build();
            }
        }
        catch (DockerCertificateException e)
        {
            log.error(e.getMessage(), e);
        }
    }
    
    /**
     * 7-23点每30秒执行
     */
    @Scheduled(cron = "0/30 * 7-23 * * ?")
    public void listContainers()
    {
        try
        {
            AtomicInteger count = new AtomicInteger();
            dockerClient.listContainers(ListContainersParam.allContainers()).forEach(container -> log.info("{}. name: {}, state {},status:{}", count.incrementAndGet(), container.names(), container.state(), container.status()));
            log.info("--------------------------");
        }
        catch (DockerException | InterruptedException e)
        {
            log.error(e.getMessage(), e);
        }
    }
    
    /**
     * 每4h执行
     */
    @Scheduled(cron = "0 0 */4 * * ?")
    public void killContainers()
    {
        try
        {
            dockerClient.listContainers(ListContainersParam.allContainers()).stream().filter(c -> StringUtils.equals(c.state(), "exited")).forEach(c -> {
                try
                {
                    log.info("#### will remove container named {}", c.names());
                    dockerClient.removeContainer(c.id());
                }
                catch (DockerException | InterruptedException e)
                {
                    log.error(e.getMessage(), e);
                }
            });
            log.info("--------------------------");
        }
        catch (DockerException | InterruptedException e)
        {
            log.error(e.getMessage(), e);
        }
    }
}
